{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8a69d69c",
   "metadata": {},
   "source": [
    "# Variational Quantum Circuit Compiling"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a24d6cf3",
   "metadata": {},
   "source": [
    "<em> Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. </em>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "596b716a",
   "metadata": {},
   "source": [
    "## Overview"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07a15fd1",
   "metadata": {},
   "source": [
    "Variational quantum circuit compilation is the process of simulating an unknown unitary operator by optimizing a parameterized quantum circuit. In this tutorial we will discuss two cases of unknown unitary operators. One is that the target $U$ is given as a matrix form, the other is that the $U$ is given as a black-box. We show how to obtain the loss function in both cases in Paddle Quantum. With auto-differentiation and optimizer provided with PaddlePaddle, we could easily approximate $U$ into a trainable sequence of quantum gates (here we use $V(\\vec{\\theta})$ to denote the unitary operator represented by the sequence of parameterized quantum gates, and for simplicity, we use $V$ below). Finally, we validate the optimized circuit by comparing the trace distance of various output density matrices transformed by the approximate circuit and the target $U$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "166a3d1c",
   "metadata": {},
   "source": [
    "## Background\n",
    "\n",
    "Earlier compilations of classical computers transformed binary numbers into electrical signals to drive the computer's electronic devices to perform operations, and then gradually developed into an assembly language for easy processing and writing. For quantum computers, similar to classical compilation, quantum compilation is a process of converting the unitary in a quantum algorithm into a series of the quantum gates to implement the algorithm. The current noisy intermediate-scale quantum (NISQ) devices have limitations such as the number of qubits, circuit depth, etc., which pose a great challenge to quantum compilation algorithms. In [1], a quantum compilation algorithm, the Quantum-assisted Quantum Compiling (QAQC), has been proposed for efficient implementation on NISQ devices. The idea of QAQC is to compile the unknown target unitary operator $U$ into the unitary $V$, define the loss function using the gate fidelity, and continuously optimize a variational quantum circuit by minimizing the loss function. But how to measure the similarity of the two unitary operators? Here we consider the probability that the unitary evolution of the $V$ can simulate the $U$, i.e., the degree of overlap between $U|\\psi\\rangle$ and $V|\\psi\\rangle$ for the input state $|\\psi\\rangle$, which is the average of the fidelity on the Haar distribution:\n",
    "\n",
    "$$\n",
    "F(U,V)=\\int_{\\psi}|\\langle\\psi|V^{\\dagger}U|\\psi\\rangle|^2d\\psi,\n",
    "\\tag{1}\n",
    "$$\n",
    "\n",
    "When $F(U,V)=1$, there is a $\\phi$ such that $V=e^{i\\phi}U$, i.e., the two unitary operators differ by a global phase factor, at which point we call $V$ an exact compilation of $U$. When $F(U,V)\\geq 1-\\epsilon$, we call $V$ an approximate compilation of $U$, where $\\epsilon$ is an error and $\\epsilon\\in[0,1]$. Based on this, we can construct the following loss function:\n",
    "\n",
    "$$\n",
    "\\begin{aligned} C(U,V)&=\\frac{d+1}{d}(1-F(U,V))\\\\\n",
    "&=1-\\frac{1}{d^2}|\\langle V,U\\rangle|^2\\\\\n",
    "&=1-\\frac{1}{d^2}|\\text{tr}(V^{\\dagger} U)|^2,\n",
    "\\end{aligned}\n",
    "\\tag{2}\n",
    "$$\n",
    "\n",
    "where $n$ is the number of qubits, $d=2^n$ and $\\frac{1}{d^2}|\\text{tr}(V^{\\dagger} U)|^2$ is the gate fidelity.\n",
    "\n",
    "From (2), we have that $C(V,U)=0$ if and only if $F(U,V)=1$, so we can obtain $V$ that approximates the target unitary operator $U$ by training a sequence of rotational gates with adjustable angles to minimize the loss function."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "189c9359",
   "metadata": {},
   "source": [
    "## The First Scenario - Matrix Form of $U$\n",
    "\n",
    "In the first case, we suppose that $U$ is given in the form of a matrix. Taking the Toffoli gate as an example, we note its matrix representation as $U_0$. We wish to construct a quantum neural network (QNN, i.e., parameterized quantum circuit) to obtain an approximate circuit decomposition of $U_0$ by training.\n",
    "\n",
    "Let us import the necessary packages:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ae8f2fdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import paddle\n",
    "import paddle_quantum\n",
    "from paddle_quantum.ansatz import Circuit\n",
    "from paddle_quantum.linalg import dagger\n",
    "from paddle_quantum.state import random_state, zero_state"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83d5d086",
   "metadata": {},
   "source": [
    "We need to get the Toffoli gate's unitary matrix:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4663732b",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 3  # Number of qubits\n",
    "\n",
    "# The matrix form of Toffoli gate\n",
    "U_0 = paddle.to_tensor(np.matrix([[1, 0, 0, 0, 0, 0, 0, 0],\n",
    "                                  [0, 1, 0, 0, 0, 0, 0, 0],\n",
    "                                  [0, 0, 1, 0, 0, 0, 0, 0],\n",
    "                                  [0, 0, 0, 1, 0, 0, 0, 0],\n",
    "                                  [0, 0, 0, 0, 1, 0, 0, 0],\n",
    "                                  [0, 0, 0, 0, 0, 1, 0, 0],\n",
    "                                  [0, 0, 0, 0, 0, 0, 0, 1],\n",
    "                                  [0, 0, 0, 0, 0, 0, 1, 0]],\n",
    "                       dtype=\"float32\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfc61ad3",
   "metadata": {},
   "source": [
    "### Constructing quantum circuits\n",
    "\n",
    "Different QNNs have different expressibility. Here we choose the `complex_entangled_layer()` function built-in in Paddle Quantum to construct QNN:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4e400e2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Constructing quantum circuit\n",
    "def qcircuit(n, D):\n",
    "    # Initialize the circuit\n",
    "    cir = Circuit(n)\n",
    "    # Call the built-in QNN template\n",
    "    cir.complex_entangled_layer('full', n, D)\n",
    "\n",
    "    return cir"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d97698f3",
   "metadata": {},
   "source": [
    "\n",
    "### Setting up the training model - loss function\n",
    "\n",
    "Next we define the loss function $C(U,V) = 1-\\frac{1}{d^2}|\\text{tr}(V^{\\dagger} U)|^2$ and training parameters in order to optimize the parameterized circuit. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "29c9ed4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the loss function\n",
    "def loss_func(cir, target_u):\n",
    "    # The matrix form of the circuit\n",
    "    V = cir.unitary_matrix()\n",
    "    # Construct Eq.(1) as the loss function\n",
    "    loss = 1 - (dagger(V).matmul(target_u).trace().abs() / V.shape[0]) ** 2\n",
    "\n",
    "    return loss "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8229500e",
   "metadata": {},
   "source": [
    "### Setting up the training model - model parameters\n",
    "\n",
    "Before training the QNN, we also need to set some training hyperparameters, mainly the depth (D) of repeated blocks, the learning rate (LR), and the number of iterations (ITR). Here we set the learning rate to 0.2 and the number of iterations to 150. The reader can adjust the hyperparameters to observe the impact on the training effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4046e5b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "D = 5  # Set the depth of QNN\n",
    "LR = 0.2  # Set the learning rate\n",
    "ITR = 150   # Set the number of optimization iterations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcde30e5",
   "metadata": {},
   "source": [
    "### Training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7103bf1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iter: 30 loss: 0.0563\n",
      "iter: 60 loss: 0.0029\n",
      "iter: 90 loss: 0.0001\n",
      "iter: 120 loss: 0.0000\n",
      "iter: 150 loss: 0.0000\n",
      "\n",
      "The trained circuit:\n",
      "--U----*---------x----U----*---------x----U----*---------x----U----*---------x----U----*---------x--\n",
      "       |         |         |         |         |         |         |         |         |         |  \n",
      "--U----x----*----|----U----x----*----|----U----x----*----|----U----x----*----|----U----x----*----|--\n",
      "            |    |              |    |              |    |              |    |              |    |  \n",
      "--U---------x----*----U---------x----*----U---------x----*----U---------x----*----U---------x----*--\n",
      "                                                                                                    \n",
      "The trained parameter theta:\n",
      " [[[[ 1.571 -0.     4.776]\n",
      "   [ 3.141  1.091  1.877]\n",
      "   [ 1.571  0.785  3.814]]\n",
      "\n",
      "  [[ 4.712  0.     3.927]\n",
      "   [ 3.142  3.404  1.833]\n",
      "   [ 1.57   3.141  6.283]]\n",
      "\n",
      "  [[ 0.786  0.     6.283]\n",
      "   [ 3.142  3.352  1.782]\n",
      "   [ 1.571  6.283  4.712]]\n",
      "\n",
      "  [[ 6.283  1.919  3.579]\n",
      "   [ 3.927  5.946  4.712]\n",
      "   [ 4.712  3.863  1.571]]\n",
      "\n",
      "  [[ 0.     1.603  5.914]\n",
      "   [ 3.142  5.805  5.469]\n",
      "   [ 3.142 -1.259  5.361]]]]\n"
     ]
    }
   ],
   "source": [
    "cir = qcircuit(n, D)\n",
    "# Using Adam optimizer to obtain relatively good convergence\n",
    "opt = paddle.optimizer.Adam(learning_rate=LR, parameters=cir.parameters())\n",
    "\n",
    "# Optimization loop\n",
    "for itr in range(1, ITR + 1):\n",
    "    loss = loss_func(cir, U_0)\n",
    "    loss.backward()\n",
    "    opt.minimize(loss)\n",
    "    opt.clear_grad()\n",
    "\n",
    "    if itr % 30 == 0:\n",
    "        print(\"iter:\", itr, \"loss:\", \"%.4f\" % loss.numpy())\n",
    "    if itr == ITR:\n",
    "        print(\"\\nThe trained circuit:\")\n",
    "        print(cir)\n",
    "\n",
    "theta_opt = cir.parameters()\n",
    "print(\"The trained parameter theta:\\n\", np.around(theta_opt, decimals=3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca9f9196",
   "metadata": {},
   "source": [
    "In this case, we construct a five-layer QNN and train it with an Adam optimizer. After around 150 iterations, the loss function reaches 0."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5f13518",
   "metadata": {},
   "source": [
    "### Validation of results\n",
    "\n",
    "In the following, we randomly select 10 density matrices, which are evolved by the target unitary operator $U$ and the approximate unitary operator $V$. Then we calculate the trace distance $ d(\\rho, \\sigma) = \\frac{1}{2}\\text{tr}\\sqrt{(\\rho-\\sigma)^{\\dagger}(\\rho-\\sigma)}$ between the real output `real_output` $\\rho$ and the approximate output `simulated_output` $\\sigma$. The smaller the trace distance, the better the approximation effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "12678dff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sample 1 :\n",
      "  trace distance is 0.00041\n",
      "sample 2 :\n",
      "  trace distance is 0.0004\n",
      "sample 3 :\n",
      "  trace distance is 0.00042\n",
      "sample 4 :\n",
      "  trace distance is 0.00045\n",
      "sample 5 :\n",
      "  trace distance is 0.00037\n",
      "sample 6 :\n",
      "  trace distance is 0.00041\n",
      "sample 7 :\n",
      "  trace distance is 0.00042\n",
      "sample 8 :\n",
      "  trace distance is 0.00039\n",
      "sample 9 :\n",
      "  trace distance is 0.00043\n",
      "sample 10 :\n",
      "  trace distance is 0.00041\n"
     ]
    }
   ],
   "source": [
    "s = 10 # Set the number of randomly generated density matrices\n",
    "\n",
    "for i in range(s):\n",
    "    paddle_quantum.set_backend('density_matrix')  # change to density matrix mode     \n",
    "    sampled = random_state(3) # randomly generated density matrix of 3 qubits sampled\n",
    "    simulated_output = paddle.matmul(paddle.matmul(cir.unitary_matrix(), sampled.data), dagger(cir.unitary_matrix())) # sampled after approximate unitary evolution\n",
    "    real_output = paddle.matmul(paddle.matmul(paddle.to_tensor(U_0), sampled.data), dagger(paddle.to_tensor(U_0))) # sampled after target unitary evolution\n",
    "    print('sample', i + 1, ':')\n",
    "    A = simulated_output.numpy() - real_output.numpy()\n",
    "    d = 1 / 2 * np.sum(np.abs(np.linalg.eigvals(A)))\n",
    "    print('  trace distance is', np.around(d, decimals=5)) # print trace distance\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "567a77a3",
   "metadata": {},
   "source": [
    "We can see that the trace distance of each sample after the evolution of $U$ and $V$ is close to 0, which means the $V$ approximates $U$ very well."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2f3d7d5",
   "metadata": {},
   "source": [
    "## The Second Scenario - Circuit Form of $U$\n",
    "\n",
    "In the second case, we suppose the $U$ needs approximation is given in the form of a black-box, and we only have access to its input and output. As a results, the fidelity can no longer be computed directly. Instead, it needs to be evaluate by a circuit.\n",
    "Next we will show how to calculate fidelity with a quantum circuit.\n",
    "\n",
    "### Calculate fidelity with a quantum circuit"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a62ac66",
   "metadata": {},
   "source": [
    "The QNN of QAQC that needs in a large quantum circuit. The whole circuit is shown below, where $U$ denotes the unitary operator to be approximated, and $V^{\\dagger}$ is the QNN we want to train. Here we use the Toffoli gate as the black-box.\n",
    "\n",
    "![circuit](./figures/vqcc-fig-circuit.png \"Figure 1: The circuit of the QAQC [1].\")\n",
    "<center>Figure 1: The circuit of the QAQC [1].</center>\n",
    "\n",
    "The circuit requires a total of $2n$ qubits, and we call the first $n$ qubits system $A$ and the last $n$ qubits system $B$. The whole circuit involves the following three steps:\n",
    "\n",
    "- First creating a maximally entangled state between $A$ and $B$ by performing Hadamard and CNOT gates.\n",
    "- Then acting with $U$ on system $A$ and with $V^{\\dagger}$ on system $B$ ($V^{\\dagger}$ is the complex conjugate of $V$), note that these two gates are performed in parallel.\n",
    "- Finally measuring in the bell basis(i.e., undoing the CNOTS and Hadamards then measuring in the standard basis).\n",
    "\n",
    "After the above operation, the probability of the full zero state obtained by the measurement is $\\frac{1}{d^2}|\\text{tr}(V^{\\dagger} U)|^2$. For a detailed explanation of Figure 1 please refer to the literature [1]."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44f2ea35",
   "metadata": {},
   "source": [
    "Here we use the same QNN that we used in the first case and use the Toffoli gate as the black-bx. \n",
    "\n",
    "Next we will implement variational quantum circuit compiling in Paddle Quantum as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d6852694",
   "metadata": {},
   "outputs": [],
   "source": [
    "n = 3 # Number of qubits\n",
    "\n",
    "# Construct the total quantum circuit\n",
    "def qcircuit(n, D):\n",
    "    \n",
    "    # Initialize the circuit of 2n qubits \n",
    "    cir = Circuit(2 * n)\n",
    "    cir.h(list(range(n)))\n",
    "    for i in range(n):\n",
    "        cir.cnot([i, n + i])\n",
    "\n",
    "    # Construct the circuit of U\n",
    "    cir.toffoli([0, 1, 2])\n",
    "\n",
    "    # Construct QNN\n",
    "    cir.complex_entangled_layer([3, 4, 5], n, D)\n",
    "    \n",
    "    for l in range(n):\n",
    "        cir.cnot([n - 1 - l, 2 * n - 1 - l])\n",
    "    cir.h(list(range(n)))\n",
    " \n",
    "    return cir"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0d141c7",
   "metadata": {},
   "source": [
    "### Setting up the training model - loss function\n",
    "\n",
    "Next we define the loss function $C(U,V) = 1-\\frac{1}{d^2}|\\text{tr}(V^{\\dagger} U)|^2$ and training parameters in order to optimize the QNN. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7ee10c61",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define loss function\n",
    "def loss_func(self):\n",
    "    paddle_quantum.set_backend('density_matrix')      \n",
    "    # Output the density matrix rho of the quantum state after the circuit\n",
    "    init_state = zero_state(2 * n)\n",
    "    rho = cir(init_state)\n",
    "    # Define loss function\n",
    "    loss = 1 - paddle.real(rho.data[0][0])\n",
    "\n",
    "    return loss"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bfa7b24",
   "metadata": {},
   "source": [
    "### Setting up the training model - model parameters\n",
    "\n",
    "Here we set the learning rate to 0.1 and the number of iterations to 120. The reader can also adjust them to observe the impact on the training effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "280e2858",
   "metadata": {},
   "outputs": [],
   "source": [
    "D = 5  # Set the depth of QNN\n",
    "LR = 0.1  # Set the learning rate\n",
    "ITR = 120   # Set the number of optimization iterations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb400510",
   "metadata": {},
   "source": [
    "### Training\n",
    "\n",
    "Then we commence the training process with an Adam optimizer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "77919a34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iter: 20 loss: 0.3032\n",
      "iter: 40 loss: 0.0080\n",
      "iter: 60 loss: 0.0011\n",
      "iter: 80 loss: 0.0002\n",
      "iter: 100 loss: 0.0001\n",
      "iter: 120 loss: 0.0000\n",
      "\n",
      "The trained circuit:\n",
      "Approximate circuit of U with circuit form input:\n",
      " --H----*--------------*-------------------------------------------------------------------------------------------------------------*----H--\n",
      "       |              |                                                                                                             |       \n",
      "--H----|----*---------*--------------------------------------------------------------------------------------------------------*----|----H--\n",
      "       |    |         |                                                                                                        |    |       \n",
      "--H----|----|----*----X---------------------------------------------------------------------------------------------------*----|----|----H--\n",
      "       |    |    |                                                                                                        |    |    |       \n",
      "-------x----|----|----U----*---------x----U----*---------x----U----*---------x----U----*---------x----U----*---------x----|----|----x-------\n",
      "            |    |         |         |         |         |         |         |         |         |         |         |    |    |            \n",
      "------------x----|----U----x----*----|----U----x----*----|----U----x----*----|----U----x----*----|----U----x----*----|----|----x------------\n",
      "                 |              |    |              |    |              |    |              |    |              |    |    |                 \n",
      "-----------------x----U---------x----*----U---------x----*----U---------x----*----U---------x----*----U---------x----*----x-----------------\n",
      "                                                                                                                                            \n"
     ]
    }
   ],
   "source": [
    "cir = qcircuit(n, D)\n",
    "# Use Adam optimizer for better performance\n",
    "opt = paddle.optimizer.Adam(learning_rate=LR, parameters=cir.parameters())\n",
    "\n",
    "# Optimization loop\n",
    "for itr in range(1, ITR + 1):\n",
    "    loss = loss_func(cir)\n",
    "    loss.backward()\n",
    "    opt.minimize(loss)\n",
    "    opt.clear_grad()\n",
    "\n",
    "    # Print training results\n",
    "    if itr % 20 == 0:\n",
    "        print(\"iter:\",itr,\"loss:\",\"%.4f\" % loss.numpy())\n",
    "    if itr == ITR:\n",
    "        print(\"\\nThe trained circuit:\")\n",
    "        print('Approximate circuit of U with circuit form input:\\n', cir)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "777ca58e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Storage optimized parameters\n",
    "theta_opt = cir.parameters()[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c65d72dd",
   "metadata": {},
   "source": [
    "In this case, we construct a one-layer QNN and train it with a Adam optimizer. After around 100 iterations, the loss function reaches 0."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "040bf7d5",
   "metadata": {},
   "source": [
    "### Validation of results\n",
    "\n",
    "Similar to before, we also randomly select 10 density matrices, which are evolved by the target unitary operator $U$ and the approximate unitary operator $V$. Then calculate the trace distance between the real output `real_output` and the approximate output `simulated_output`. The smaller the trace distance, the better the approximation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0c339ca3",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sample 1 :\n",
      "  trace distance is 0.00245\n",
      "sample 2 :\n",
      "  trace distance is 0.00239\n",
      "sample 3 :\n",
      "  trace distance is 0.00236\n",
      "sample 4 :\n",
      "  trace distance is 0.00222\n",
      "sample 5 :\n",
      "  trace distance is 0.00235\n",
      "sample 6 :\n",
      "  trace distance is 0.00251\n",
      "sample 7 :\n",
      "  trace distance is 0.00241\n",
      "sample 8 :\n",
      "  trace distance is 0.00248\n",
      "sample 9 :\n",
      "  trace distance is 0.00228\n",
      "sample 10 :\n",
      "  trace distance is 0.00242\n"
     ]
    }
   ],
   "source": [
    "s = 10 # Set the number of randomly generated density matrices\n",
    "for i in range(s):\n",
    "    paddle_quantum.set_backend('density_matrix')  # change to density matrix mode    \n",
    "    sampled = random_state(3) # randomly generated density matrix of 4 qubits sampled\n",
    "\n",
    "    # Construct the circuit of target unitary\n",
    "    cir_1 = Circuit(3)\n",
    "    cir.toffoli([0, 1, 2])\n",
    "    # sampled after target unitary evolution\n",
    "    real_output = paddle.matmul(paddle.matmul(cir_1.unitary_matrix(), sampled.data), dagger(cir_1.unitary_matrix()))\n",
    "\n",
    "    # Construct the circuit of approximate unitary\n",
    "    cir_2 = Circuit(3)\n",
    "    for j in range(D):\n",
    "        cir_2.u3(qubits_idx='full', num_qubits=3, param=theta_opt[j])\n",
    "        cir_2.cnot(qubits_idx='cycle')\n",
    "    # sampled after approximate unitary evolution\n",
    "    simulated_output = paddle.matmul(paddle.matmul(cir_2.unitary_matrix(), sampled.data), dagger(cir_2.unitary_matrix()))\n",
    "\n",
    "    A = simulated_output.numpy() - real_output.numpy()\n",
    "    d = 1 / 2 * np.sum(np.abs(np.linalg.eigvals(A)))\n",
    "    print('sample', i + 1, ':')\n",
    "    print('  trace distance is',  np.around(d, decimals=5)) # print trace distance"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8b3d6c4",
   "metadata": {},
   "source": [
    "We can see that the trace distance of each sample after the evolution of $U$ and $V$ is close to 0, which means the $V$ approximates $U$ very well."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afe8ea7c",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "In this tutorial, the variational quantum circuit compiling is carried out from the input form of the target unitary operator as a matrix and as a circuit. The results of the quantum compilation are demonstrated by two simple examples using Paddle Quantum. Then the approximate effect is checked by the trace distance of the quantum states after the evolution of the target unitary and the approximate unitary respectively. Finally the results in Paddle Quantum show that the quantum compilation is good."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc610a2f",
   "metadata": {},
   "source": [
    "_______\n",
    "\n",
    "## References"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62dd936d",
   "metadata": {},
   "source": [
    "[1] Khatri, Sumeet, et al. \"Quantum-assisted quantum compiling.\" [Quantum 3 (2019): 140](https://quantum-journal.org/papers/q-2019-05-13-140/)."
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "f7cfecff1ef1940b21a48efa1b88278bb096bd916f13c2df11af4810c38b47e1"
  },
  "kernelspec": {
   "display_name": "Python 3.8.0 ('pq')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
